﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace JustinAngel.MetroBlog.Common.Helpers
{
    public static class RazorMvcHelper
    {
        public static void SetWebsiteRoot(this Controller controller)
        {
            var returnurl = controller.Request.Url.AbsoluteUri;
            var extraBitAtTheEnd = controller.Request.Url.PathAndQuery;
            if (extraBitAtTheEnd != "/")
                returnurl = returnurl.Remove(extraBitAtTheEnd);
            else
                returnurl = returnurl.TrimEnd('/');

            controller.ViewData["WebsiteRoot"] = returnurl;
        }

        public static MvcHtmlString WebsiteRootEscaped(this HtmlHelper helper)
        {
            return new MvcHtmlString(HttpUtility.UrlEncode(helper.ViewData["WebsiteRoot"].ToString()));
        }

        public static MvcHtmlString WebsiteRoot(this HtmlHelper helper)
        {
            return new MvcHtmlString(helper.ViewData["WebsiteRoot"].ToString());
        }
    }
}
